<template>
  <div class="header">
    <el-container>
      <el-header>
        <el-row>
          <el-col class="logo" :span="3">
            <a href="/">
              <img src="@/assets/head-logo.svg" alt="">
            </a>
          </el-col>
          <el-col class="nav" :span="16">
            <el-row>
              <el-col :span="3" v-for="nav in nav_list">
                <a v-if="nav.link.startsWith('http')" :class="check(nav.link)?'current':''" :href="nav.link">{{nav.name}}</a>
                <router-link v-else :class="check(nav.link)?'current':''" :to="nav.link">{{nav.name}}</router-link>
              </el-col>
            </el-row>
          </el-col>
          <el-col class="login-bar" :span="5">
            <el-row v-if="this.$store.state.user.token">
              <el-col class="cart-ico" :span="9">
                <router-link to="">
                  <b class="goods-number">{{this.$store.state.cart.count}}</b>
                  <img class="cart-icon" src="@/assets/cart.svg" alt="">
                  <span><router-link to="/cart">购物车</router-link></span>
                </router-link>
              </el-col>
              <el-col class="study" :span="8" :offset="2">
                <router-link to="">学习中心</router-link>
              </el-col>
              <el-col class="member" :span="5">
                <el-menu class="el-menu-demo" mode="horizontal">
                  <el-submenu index="2">
                    <template slot="title">
                      <router-link to=""><img src="@/assets/logo@2x.png" alt=""></router-link>
                    </template>
                    <el-menu-item index="2-1">我的账户</el-menu-item>
                    <el-menu-item index="2-2">
                      <router-link to="/my/order">我的订单</router-link>
                    </el-menu-item>
                    <el-menu-item index="2-3">我的优惠卷</el-menu-item>
                    <el-menu-item index="2-3" @click="logout">退出登录</el-menu-item>
                  </el-submenu>
                </el-menu>
              </el-col>
            </el-row>
            <el-row v-else>
              <el-col class="cart-ico" :span="9">
                <router-link to="">
                  <img class="cart-icon" src="@/assets/cart.svg" alt="">
                  <span><router-link to="/cart">购物车</router-link></span>
                </router-link>
              </el-col>
              <el-col :span="10" :offset="5">
                <span class="register">
                  <router-link to="/login">登录</router-link>
                  &nbsp;&nbsp;|&nbsp;&nbsp;
                  <router-link to="/register">注册</router-link>
                </span>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
      </el-header>
    </el-container>
  </div>
</template>

<script>
  export default {
    name: "Header",
    props: ['current_page', 'is_login'],
    data() {
      return {
        // token: sessionStorage.token || localStorage.token,
        // user_name: sessionStorage.username || localStorage.username,
        // user_id: sessionStorage.id || localStorage.id,
        nav_list: [],
      };
    },
    created() {
      this.$axios.get(this.$settings.Host + '/nav/').then(response => {
        this.nav_list = response.data;
      }).catch(error => {
        console.log(error.response)
      });
      if (this.$store.state.user.token) {
        this.$axios.get(this.$settings.Host + '/cart/course/', {
          headers: {
            'Authorization': 'jwt ' + this.$store.state.user.token
          }
        }).then(response => {
          this.courseData = response.data;
          this.$store.state.cart.count = response.data.length;
        }).catch(error => {
          let status = error.response.status;
          if (status === 403 || status === 401) {
            console.log('header_cart_course_401或403错误')
          }
        });
      }

    },
    methods: {
      check(link) {
        return link == window.location.pathname
      },
      logout() {
        // this.token = false;
        // this.user_id = false;
        // this.user_name = false;
        localStorage.removeItem('token');
        localStorage.removeItem('id');
        localStorage.removeItem('username');
        sessionStorage.removeItem('token');
        sessionStorage.removeItem('id');
        sessionStorage.removeItem('username');
        this.$store.state.user.token = '';
        this.$store.state.user.id = '';
        this.$store.state.user.username = '';
        this.$alert('退出登录成功', '路飞学城', {
          confirmButtonText: '确定'
        })
      }
    }
  }
</script>

<style scoped>
  .header {
    top: 0;
    left: 0;
    right: 0;
    margin: auto;
    background-color: #fff;
    height: 80px;
    z-index: 1000;
    position: fixed;
    box-shadow: 0 0.5px 0.5px 0 #c9c9c9;
  }

  .header .el-container {
    width: 1200px;
    margin: 0 auto;
  }

  .el-header {
    height: 80px !important;
    padding: 0;
  }

  .logo {

  }

  .logo img {
    padding-top: 22px;
    width: 118px
  }

  .nav {
    margin-top: 22px;
  }

  .nav .el-col a {
    display: inline-block;
    text-align: center;
    padding-bottom: 16px;
    padding-left: 5px;
    padding-right: 5px;
    position: relative;
    font-size: 16px;
    margin-left: 20px;
  }

  .nav .el-col .current {
    color: #4a4a4a;
    border-bottom: 4px solid #ffc210;
  }

  .login-bar {
    margin-top: 22px;
  }

  .cart-ico {
    position: relative;
    border-radius: 17px;
    background-color: #f6f6f6;
  }

  .cart-ico:hover {
    background: #f0f0f0;
  }

  .goods-number {
    width: 16px;
    height: 16px;
    line-height: 17px;
    font-size: 12px;
    color: #fff;
    text-align: center;
    background: #fa6240;
    border-radius: 50%;
    transform: scale(.8);
    position: absolute;
    left: 16px;
    top: -1px;
  }

  .cart-icon {
    width: 15px;
    height: auto;
    margin-left: 6px;
  }

  .cart-ico span {
    margin-left: 12px;
  }

  .member img {
    width: 26px;
    height: 26px;
    border-radius: 50%;
    display: inline-block;
  }

  .member img:hover {
    border: 1px solid yellow;
  }

  .el-menu.el-menu--horizontal {
    border-bottom: none;

  }

  .el-submenu__title i {
    display: none;
  }


</style>
